<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/25
 * Time: 10:41
 */

namespace App\Http\Controllers\Agentadmin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class MyController extends Controller
{
    //我的信息展示
    public function myinfo(){
        $user_id = session('user_id');
        $brokerage = \DB::table('user')->where(['user_id'=>trim($user_id)])->value('brokerage');
        $teamnum = \DB::table('user')->where(['pid'=>trim($user_id)])->orwhere('pid','like','%'.','.trim($user_id))->count();
        return view('agentadmin.my.myinfo',compact('brokerage','teamnum'));
    }

    //提现页面
    public function withdraw(Request $request){

        $user_id = session('user_id');
            return view('agentadmin.my.withdraw',compact('user_id'));

    }
    //提现操作
    public function operacash(Request $request){
        $user_id = session('user_id');
        $gold = \DB::table('user')->where(['user_id'=>$user_id])->value('brokerage');
        $data = $request->all();
        if ($data['money'] > $gold){
            $array = array('msg'=>'提现金额不能大于总金额','status'=>'false');
            return $array;
        }
        $addid = \DB::table('cash')->insertGetId(
            [
                'user_id' => $user_id,
                'account' => $data['account'],
                'money' => $data['money'],
                'apply_time' => date('Y-m-d H:i:s',time()),
                'user_name'=>\DB::table('user')->where(['user_id'=>$user_id])->value('user_name'),
                'user_phone'=>\DB::table('user')->where(['user_id'=>$user_id])->value('phone'),
            ]
        );
        if($addid){
            \DB::table('user')->where(['user_id'=>$user_id])->decrement('brokerage',$data['money']);
            $array = array('msg'=>'提交成功,请等待管理员审核!','status'=>'true');
            return $array;
        }else{
            $array = array('msg'=>'提交失败,请重试!','status'=>'false');
            return $array;
        }
    }
}